package com.menchao.carsales.common.http;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Porject: carsales
 * Date: 2021/7/13
 * Author: menchao
 * Description:
 */


//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseData<T> {

    private int code;
    private String message;
    private T data;


    private ResponseData(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseData(int code, String message){
        this.code = code;
        this.message = message;
    }


    public ResponseData(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @JsonIgnore
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }


    public static <T> ResponseData<T> success(T data) {
        return new ResponseData<T>(ResponseCode.SUCCESS.getCode(), "成功",data);
    }

    public static <T> ResponseData<T> success() {
        return new ResponseData<T>(ResponseCode.SUCCESS.getCode(), "成功",null);
    }

    public static <T> ResponseData<T> faile(String faile) {
        return new ResponseData<T>(ResponseCode.FAIL.getCode(), faile,null);
    }



    public static <T> ResponseData<T> success(String message, T data) {
        return new ResponseData<T>(ResponseCode.SUCCESS.getCode(),message,data);
    }

    public static <T> ResponseData<T> errorMessage(String errorMessage) {
        return new ResponseData<T>(ResponseCode.USER_NOT_FOUND.getCode(),errorMessage);
    }

    public static <T> ResponseData<T> errorMessage(String errorMessage,int code) {
        return new ResponseData<T>(code,errorMessage);
    }

    public static <T> ResponseData<T> errorMessageForEmptyUserOrPassword(String errorMessage) {
        return new ResponseData<T>(ResponseCode.ERROR_AUTH.getCode(),errorMessage);
    }


    @Override
    public String toString() {
        return "ResponseData{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
